package com.lenovo.smarttraffic.zjh;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import com.lenovo.smarttraffic.R;
import com.lenovo.smarttraffic.ui.activity.BaseActivity;

import static com.lenovo.smarttraffic.InitApp.IS_IPSET;

public class Setting_Activity extends BaseActivity {
    private EditText editText4;
    private EditText editText;
    private EditText editText2;
    private EditText editText3;
    private Button btnSetting;
    public static String IP;
    public static String URL_My;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        initToolBar("IP设置");
        initView();
    }

    private void initView() {
        editText4 = (EditText) findViewById(R.id.editText4);
        editText = (EditText) findViewById(R.id.editText);
        editText2 = (EditText) findViewById(R.id.editText2);
        editText3 = (EditText) findViewById(R.id.editText3);
        btnSetting = (Button) findViewById(R.id.btn_Setting);

        editText.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                if (s.length() == 0) {

                } else {
                    if (Integer.parseInt(editText.getText().toString()) > 999 || Integer.parseInt(editText.getText().toString()) < 1) {
                        s = "";
                        editText.setText(s);
                        Toast.makeText(Setting_Activity.this, "请输入正确的IP", Toast.LENGTH_SHORT).show();
                    }
                }
            }

            @Override
            public void afterTextChanged(Editable s) {

            }
        });

        editText2.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                if (s.length() == 0) {

                } else {
                    if (Integer.parseInt(editText2.getText().toString()) > 999 || Integer.parseInt(editText2.getText().toString()) < 1) {
                        s = "";
                        editText2.setText(s);
                        Toast.makeText(Setting_Activity.this, "请输入正确的IP", Toast.LENGTH_SHORT).show();
                    }
                }
            }

            @Override
            public void afterTextChanged(Editable s) {

            }
        });

        editText3.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                if (s.length() == 0) {

                } else {
                    if (Integer.parseInt(editText3.getText().toString()) > 999 || Integer.parseInt(editText3.getText().toString()) < 1) {
                        s = "";
                        editText3.setText(s);
                        Toast.makeText(Setting_Activity.this, "请输入正确的IP", Toast.LENGTH_SHORT).show();
                    }
                }
            }

            @Override
            public void afterTextChanged(Editable s) {

            }
        });

        editText4.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                if (s.length() == 0) {

                } else {
                    if (Integer.parseInt(editText4.getText().toString()) > 999 || Integer.parseInt(editText4.getText().toString()) < 1) {
                        s = "";
                        editText4.setText(s);
                        Toast.makeText(Setting_Activity.this, "请输入正确的IP", Toast.LENGTH_SHORT).show();
                    }
                }
            }

            @Override
            public void afterTextChanged(Editable s) {

            }
        });

        btnSetting.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (!editText.getText().toString().isEmpty() &&
                        !editText2.getText().toString().isEmpty() &&
                        !editText3.getText().toString().isEmpty() &&
                        !editText4.getText().toString().isEmpty()) {
                    IS_IPSET = true;
                    IP = editText.getText().toString() + "." +
                            editText2.getText().toString() + "." +
                            editText3.getText().toString() + "." +
                            editText4.getText().toString();
                    Toast.makeText(Setting_Activity.this, IP, Toast.LENGTH_SHORT).show();
                    URL_My = "http://" + IP + ":8088/transportservice/action/";
                    finish();
                } else {
                    Toast.makeText(Setting_Activity.this, "IP不能为空！", Toast.LENGTH_SHORT).show();
                }
            }
        });

    }

    @Override
    protected int getLayout() {
        return R.layout.layout_setting;
    }
}
